<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="/static/js/jquery-1.12.4.min.js"></script>
    <script src="/static/js/qrcode.min.js"></script>
</head>
<style>
    body,
    .mainWindow {
        width: 310px;
        height: 410px;
        padding: 0;
        margin: 0;
        border: 0;
        color: #FEFEFE;
    }

    .codeshow {
        height: 100%;
        margin: 5px;
        border: #CC6600 2px solid;
        border-radius: 10px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;
        background-color: #CC3399;
    }

    .title {
        font-size: large;
    }

    .title {
        font-size: small
    }

    .reqsn {
        font-size: small;
    }

    .back {
        padding: 5px 25px;
        border: #CC6600 2px solid;
        border-radius: 5px;
        background-color: #3366CC;
        color: #FEFEFE;
    }

    #result4 {
        margin: 2px;
        padding: 2px;
        border: #FEFEFE 2px solid;
        background-color: #FEFEFE;
    }
</style>

{% load webtags %}

<body>
    <div class="mainWindow">
        <div class="codeshow">
            {% csrf_token %}
            <span class="title">{{sess.placeid}}</span>
            <span class="title2">{{sess.termsn}}</span>
            <div id="result4"></div>
            <span class="reqsn">订单号: {{sess.reqsn}}</span>
            <span class="trxamt">交易金额: {% toYuan sess.trxamt %} 元</span>
            <span class="resmsg"></span>
            <input class="back" type="button" value="返回">
        </div>
    </div>
</body>
<script>
    function CloseWebPage() {
        if (navigator.userAgent.indexOf("MSIE") > 0) {
            if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
                window.opener = null; window.close();
            } else {
                window.open('', '_top'); window.top.close();
            }
        }
        else if (navigator.userAgent.indexOf("Firefox") > 0) {
            window.location.href = 'about:blank '; //火狐默认状态非window.open的页面window.close是无效的
            //window.history.go(-2);
        } else {
            window.opener = null;
            window.open('', '_self', '');
            window.close();
        }
    }
    $(document).on("ready", function () {
        let obj = "{{sess}}";
        if (obj == "None") {
            alert("订单无效或已超时，页面将关闭...");
            CloseWebPage();
        } else {
            // <!-- 创建二维码 -->
            let url = "{{sess.payinfo}}";
            let qrcode = new QRCode("result4", {
                text: url,
                width: 180,
                height: 180,
                colorDark: "#000000",
                colorLight: "#ffffff",
                correctLevel: QRCode.CorrectLevel.H
            });

            let pay = "{{sess.paytype}}";
            if (pay == "W01") {
                $(".codeshow").css("background-color", "#07C160");
            } else if (pay == "A01") {
                $(".codeshow").css("background-color", "#226BF3");
            } else if (pay == "U01") {
                $(".codeshow").css("background-color", "#F8281D");
            } else { }

            var csrf = $('input[name="csrfmiddlewaretoken"]').val();

            var id = setInterval(function () {
                $.post("/loan/transquery/", { "reqsn": "{{sess.reqsn}}", "csrfmiddlewaretoken": csrf }, function (res, status) {
                    console.log(res);
                    if (!res.rescode == 200) { alert("服务器错误！"); CloseWebPage(); }
                    if (res.resmsg.retcode == "SUCCESS") {
                        if (res.resmsg.trxstatus == "0000") {
                            $('.resmsg').text("交易成功");
                            clearInterval(id);
                            alert("交易成功！");
                            window.opener = null;
                            window.close();
                        }
                    }
                });
            }, 4000);

            $(".back").on("click", function () {
                clearInterval(id);
                CloseWebPage();
                // window.history.back();
            });

        }
    });
</script>

</html>